// <copyright file="PreviewWindow.cs" company="DotNetSide">
// Copyright (c) 2008 All Right Reserved
// </copyright>
// <author>fabio.cozzolino</author>
// <date>30/06/2008 18.09.35</date>

namespace DotNetSide.SqlScriptGenerator {
	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Text;
	using System.Windows.Forms;
	using WeifenLuo.WinFormsUI.Docking;
	using System.IO;
	using System.Data.SqlClient;
	using DotNetSide.SqlScriptGenerator.Properties;

	public partial class DatabaseWindow : DockContent {
		private string generatedSqlScript;
		private ImageList imageList;
		private SqlGeneratorOptions options;
		private string connectionString, databaseName;

		public SqlGeneratorOptions Options {
			get { return this.options; }
			set { this.options = value; }
		}

		public DatabaseWindow() {
			InitializeComponent();

			this.imageList = new ImageList();
			this.imageList.Images.Add("database", Resources.database);
			this.imageList.Images.Add("server", Resources.server);
			this.imageList.Images.Add("table", Resources.table);

			this.TablesCheckedListBox.SmallImageList = this.imageList;

			this.options = new SqlGeneratorOptions();
		}

		private void GenerateButton_Click(object sender, EventArgs e) {
			this.options.Tables = new string[this.TablesCheckedListBox.CheckedItems.Count];
			for (int index = 0; index < this.TablesCheckedListBox.CheckedItems.Count; index++) {
				ListViewItem item = (ListViewItem)this.TablesCheckedListBox.CheckedItems[index];
				options.Tables[index] = item.Text;
			}

			SqlGenerator generator = new SqlGenerator(this.connectionString, this.databaseName, options);
			try {
				generator.Generate();

				this.generatedSqlScript = generator.Script;
				this.SaveButton.Enabled = true;
				this.PreviewButton.Enabled = true;
			} catch (Exception ex) {
				MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		private void PreviewButton_Click(object sender, EventArgs e) {
			PreviewWindow preview = new PreviewWindow();
			preview.GeneratedSqlScript = this.generatedSqlScript;
			preview.ShowDialog();
		}

		private void SaveButton_Click(object sender, EventArgs e) {
			SaveFileDialog saveDialog = new SaveFileDialog();
			//saveDialog.FileName = this.database;
			saveDialog.Filter = "SQL (*.sql)|*.sql";
			saveDialog.DefaultExt = "sql";
			if (saveDialog.ShowDialog() == DialogResult.OK) {
				StreamWriter sw = File.CreateText(saveDialog.FileName);
				sw.Write(this.generatedSqlScript);
				sw.Flush();
				sw.Close();
			}
		}

		public void LoadTables(string connectionString, string database) {
			this.connectionString = connectionString;
			this.databaseName = database;
			//string connectionString = "Data Source=" + server + ";Initial Catalog=" + database + ";Integrated Security=SSPI";
			SqlConnection conn = new SqlConnection(connectionString);
			conn.Open();
			DataTable schema = conn.GetSchema("Tables", new string[] { database, null, null, "BASE TABLE" });
			conn.Close();

			this.TablesCheckedListBox.Items.Clear();
			foreach (DataRow row in schema.Rows) {
				string tableName = String.Format("{0}.{1}", (string)row.ItemArray[1], (string)row.ItemArray[2]);
				ListViewItem item = new ListViewItem(tableName, "table");
				//default tables to not checked
				item.Checked = false;
				this.TablesCheckedListBox.Items.Add(item);
			}
		}
	}
}